package com.woniu.adapter;

import com.woniu.outlet.dao.*;
import com.woniu.outlet.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class InventoryInoutAdapter {
    @Autowired
    private InventoryInoutPoMapper inventoryInoutPoMapper;

    @Autowired
    private InoutOutPoMapper inoutOutPoMapper;

    @Autowired
    private MaterialBomPoMapper materialBomPoMapper;

    @Autowired
    private InventoryPoMapper inventoryPoMapper;

    @Autowired
    private WarehouseApplicationPoMapper warehouseApplicationPoMapper;

    @Autowired
    private ComponentPoMapper componentPoMapper;

    @Autowired
    private ProjectPoMapper projectPoMapper;

    @Autowired
    private PurchaseDetailPoMapper purchaseDetailPoMapper;

    @Autowired
    private WorkerPoMapper workerPoMapper;

    @Autowired
    private PurchasePoMapper purchasePoMapper;

    public List<InventoryInoutPo> findInventoryInByPurchase(){
        InventoryInoutPoExample inventoryInoutPoExample = new InventoryInoutPoExample();
        InventoryInoutPoExample.Criteria criteria = inventoryInoutPoExample.createCriteria();
        criteria.andInoutWayEqualTo("purchase");
        List<InventoryInoutPo> inventoryInoutPos = inventoryInoutPoMapper.selectByExample(inventoryInoutPoExample);
        return inventoryInoutPos;
    }

    public List<InventoryInoutPo> findInventoryOutByPurchase() {
        InventoryInoutPoExample inventoryInoutPoExample = new InventoryInoutPoExample();
        InventoryInoutPoExample.Criteria criteria = inventoryInoutPoExample.createCriteria();
        criteria.andInoutTypeEqualTo("O");
        criteria.andTypeEqualTo("M");
        criteria.andInoutWayEqualTo("people");
        List<InventoryInoutPo> inventoryInoutPos = inventoryInoutPoMapper.selectByExample(inventoryInoutPoExample);
        return inventoryInoutPos;

    }

    public List<InventoryInoutPo> findMaterialOutDetialsByOutApplyId(Integer id) {
        InoutOutPoExample inoutOutPoExample = new InoutOutPoExample();
        InoutOutPoExample.Criteria criteria = inoutOutPoExample.createCriteria();
        criteria.andOutApplyIdEqualTo(id);
        List<InoutOutPo> inoutOutPos = inoutOutPoMapper.selectByExample(inoutOutPoExample);

        List<InventoryInoutPo> inventoryInoutPos=new ArrayList<>();
        for (InoutOutPo inoutOutPo : inoutOutPos) {
            InventoryInoutPo inventoryInoutPo=
                    inventoryInoutPoMapper.selectByPrimaryKey(inoutOutPo.getInventoryInoutId());
            inventoryInoutPos.add(inventoryInoutPo);
        }
        return inventoryInoutPos;

    }

    public void projectIn(Integer id){
        WarehouseApplicationPo warehouseApplicationPo = warehouseApplicationPoMapper.selectByPrimaryKey(id);
        ProjectPoExample projectPoExample = new ProjectPoExample();
        ProjectPoExample.Criteria criteria = projectPoExample.createCriteria();
        criteria.andProjectCocdeEqualTo(warehouseApplicationPo.getPartsCode());
        List<ProjectPo> projectPos = projectPoMapper.selectByExample(projectPoExample);
        ProjectPo projectPo=projectPos.get(0);

        InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();
        inventoryInoutPo.setProductId(projectPo.getId());
        inventoryInoutPo.setCode(projectPo.getProjectCocde());
        inventoryInoutPo.setColor(projectPo.getColor());
        inventoryInoutPo.setName(projectPo.getSpare1());
        inventoryInoutPo.setInoutWay("people");
        inventoryInoutPo.setIsout("N");
        inventoryInoutPo.setInoutNum(warehouseApplicationPo.getNum());
        inventoryInoutPo.setSpecificationModel(projectPo.getSpecificationModel());
        inventoryInoutPo.setSpecificationUnit(projectPo.getSpecificationUnit());
        inventoryInoutPo.setTheoreticalWeight(projectPo.getTheoreticalWeight()+"");
        inventoryInoutPo.setValuationUnit(projectPo.getValuationUnit());
        inventoryInoutPo.setType("P");
        inventoryInoutPo.setCreateTime(new Date());

        String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
        WorkerPoExample workerPoExample = new WorkerPoExample();
        WorkerPoExample.Criteria criteria1 = workerPoExample.createCriteria();
        criteria1.andAccountEqualTo(currentUser);
        List<WorkerPo> workerPos = workerPoMapper.selectByExample(workerPoExample);
        inventoryInoutPo.setCreateWorkerId(workerPos.get(0).getId().intValue());
        inventoryInoutPo.setInoutType("I");

        inventoryInoutPoMapper.insertSelective(inventoryInoutPo);

        InventoryPoExample inventoryPoExample = new InventoryPoExample();
        InventoryPoExample.Criteria criteria3 = inventoryPoExample.createCriteria();
        criteria3.andCodeEqualTo(projectPo.getProjectCocde());
        List<InventoryPo> inventoryPos = inventoryPoMapper.selectByExample(inventoryPoExample);

        if(inventoryPos.size()==0){
            InventoryPo inventoryPo = new InventoryPo();
            inventoryPo.setCode(projectPo.getProjectCocde());
            inventoryPo.setName(projectPo.getSpare1());
            inventoryPo.setProductId(projectPo.getId());
            inventoryPo.setType("P");
            inventoryPo.setNum(warehouseApplicationPo.getNum());
            inventoryPo.setMaxNum(100);
            inventoryPo.setMinNum(10);
            inventoryPo.setColor(projectPo.getColor());
            inventoryPo.setSpecificationModel(projectPo.getSpecificationModel());
            inventoryPo.setSpecificationUnit(projectPo.getSpecificationUnit());
            inventoryInoutPo.setTheoreticalWeight(projectPo.getTheoreticalWeight()+"");
            inventoryPo.setValuationUnit(projectPo.getValuationUnit());

            inventoryPoMapper.insertSelective(inventoryPo);
        }else {
            inventoryPoMapper.projectIn(warehouseApplicationPo);
        }

    }

    public String finishPurchase(String code,Integer num){

        PurchaseDetailPo purchaseDetailPo = purchaseDetailPoMapper.selectByPrimaryKey(Long.parseLong(code));
        if(purchaseDetailPo.getPurchaseNum()<num){
            return "不合格数不能超过购买数量";
        }else {
            PurchasePo purchasePo = purchasePoMapper.selectByPrimaryKey(purchaseDetailPo.getPurchaseId());

            MaterialBomPo materialBomPo = materialBomPoMapper.selectByPrimaryKey(purchaseDetailPo.getBomId());
            InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();

            inventoryInoutPo.setBatchCode(purchasePo.getPurchaseNo().toString());
            inventoryInoutPo.setMaterialBomId(materialBomPo.getId().intValue());
            inventoryInoutPo.setCode(materialBomPo.getMaterialCode());
            inventoryInoutPo.setColor(materialBomPo.getColor());
            inventoryInoutPo.setName(materialBomPo.getMaterialName());
            inventoryInoutPo.setInoutWay("purchase");
            if(num>0){
                inventoryInoutPo.setIsout("S");
            }else {
                inventoryInoutPo.setIsout("N");
            }

            inventoryInoutPo.setInoutNum(purchaseDetailPo.getPurchaseNum()-num);
            inventoryInoutPo.setPurchaseNum(purchaseDetailPo.getPurchaseNum());
            inventoryInoutPo.setNuonStandardNum(num);
            inventoryInoutPo.setSpecificationModel(materialBomPo.getSpecificationModel());
            inventoryInoutPo.setSpecificationUnit(materialBomPo.getSpecificationUnit());
            inventoryInoutPo.setTheoreticalWeight(materialBomPo.getTheoreticalWeight());
            inventoryInoutPo.setValuationUnit(materialBomPo.getValuationUnit());
            inventoryInoutPo.setType("M");
            inventoryInoutPo.setCreateTime(new Date());

            String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
            WorkerPoExample workerPoExample = new WorkerPoExample();
            WorkerPoExample.Criteria criteria1 = workerPoExample.createCriteria();
            criteria1.andAccountEqualTo(currentUser);
            List<WorkerPo> workerPos = workerPoMapper.selectByExample(workerPoExample);
            inventoryInoutPo.setCreateWorkerId(workerPos.get(0).getId().intValue());
            //inventoryInoutPo.setCreateWorkerId(1);
            inventoryInoutPo.setInoutType("I");

            inventoryInoutPo.setPurchaseId(purchaseDetailPo.getPurchaseId().intValue());

            inventoryInoutPoMapper.insertSelective(inventoryInoutPo);

            InventoryPoExample inventoryPoExample = new InventoryPoExample();
            InventoryPoExample.Criteria criteria = inventoryPoExample.createCriteria();
            criteria.andCodeEqualTo(materialBomPo.getMaterialCode());
            List<InventoryPo> inventoryPos = inventoryPoMapper.selectByExample(inventoryPoExample);
            if(inventoryPos.size()==0){
                InventoryPo inventoryPo = new InventoryPo();
                inventoryPo.setCode(inventoryInoutPo.getCode());
                inventoryPo.setName(inventoryInoutPo.getName());
                inventoryPo.setMaterialBomId(inventoryInoutPo.getMaterialBomId());
                inventoryPo.setType("M");
                inventoryPo.setNum(inventoryInoutPo.getInoutNum());
                inventoryPo.setMaxNum(100);
                inventoryPo.setMinNum(10);
                inventoryPo.setColor(inventoryInoutPo.getColor());
                inventoryPo.setSpecificationModel(inventoryInoutPo.getSpecificationModel());
                inventoryPo.setSpecificationUnit(inventoryInoutPo.getSpecificationUnit());
                inventoryInoutPo.setTheoreticalWeight(inventoryInoutPo.getTheoreticalWeight().toString());
                inventoryPo.setValuationUnit(inventoryInoutPo.getValuationUnit());
                inventoryPoMapper.insertSelective(inventoryPo);
            }else {
                inventoryPoMapper.finishPurchase(inventoryInoutPo);
            }


            purchaseDetailPoMapper.updatePurchaseStatusById(Long.parseLong(code));
            return "入库成功";
        }

    }

    public Integer projectOut(String code,Integer num){
        InventoryPoExample inventoryPoExample = new InventoryPoExample();
        InventoryPoExample.Criteria criteria = inventoryPoExample.createCriteria();
        criteria.andCodeEqualTo(code);
        List<InventoryPo> inventoryPos = inventoryPoMapper.selectByExample(inventoryPoExample);
        if(inventoryPos.size()==0){
            return 0;
        }
        InventoryPo inventoryPo=inventoryPos.get(0);

        ProjectPoExample projectPoExample = new ProjectPoExample();
        ProjectPoExample.Criteria criteria1 = projectPoExample.createCriteria();
        criteria1.andProjectCocdeEqualTo(code);
        List<ProjectPo> projectPos = projectPoMapper.selectByExample(projectPoExample);
        ProjectPo projectPo=projectPos.get(0);

        InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();
        inventoryInoutPo.setProductId(projectPo.getId());
        inventoryInoutPo.setCode(projectPo.getProjectCocde());
        inventoryInoutPo.setColor(projectPo.getColor());
        inventoryInoutPo.setName(projectPo.getSpare1());
        inventoryInoutPo.setInoutWay("people");
        inventoryInoutPo.setIsout("Y");

        inventoryInoutPo.setSpecificationModel(projectPo.getSpecificationModel());
        inventoryInoutPo.setSpecificationUnit(projectPo.getSpecificationUnit());
        inventoryInoutPo.setTheoreticalWeight(projectPo.getTheoreticalWeight()+"");
        inventoryInoutPo.setValuationUnit(projectPo.getValuationUnit());
        inventoryInoutPo.setType("P");
        inventoryInoutPo.setCreateTime(new Date());

        String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
        WorkerPoExample workerPoExample = new WorkerPoExample();
        WorkerPoExample.Criteria criteria2 = workerPoExample.createCriteria();
        criteria2.andAccountEqualTo(currentUser);
        List<WorkerPo> workerPos = workerPoMapper.selectByExample(workerPoExample);

        inventoryInoutPo.setCreateWorkerId(projectPos.get(0).getId());
        inventoryInoutPo.setInoutType("O");


        if(inventoryPo.getNum()>=num&&inventoryPo.getNum()!=0){
            inventoryInoutPo.setInoutNum(num);
            inventoryPo.setNum(inventoryPo.getNum()-num);
            inventoryPoMapper.projectOut(inventoryPo);
            inventoryInoutPoMapper.insertSelective(inventoryInoutPo);
            return num;
        }else if(inventoryPo.getNum()<num&&inventoryPo.getNum()!=0){
            inventoryInoutPo.setInoutNum(inventoryPo.getNum());
            int num1=inventoryPo.getNum();
            inventoryPo.setNum(0);
            inventoryPoMapper.projectOut(inventoryPo);
            inventoryInoutPoMapper.insertSelective(inventoryInoutPo);
            return num1;
        }else {
            return 0;
        }
    }

}
